package com.global.college.base;

import android.content.res.Configuration;
import android.os.Bundle;
import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;
import androidx.fragment.app.FragmentActivity;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.gyf.barlibrary.ImmersionBar;
import com.global.college.R;
import com.global.college.utils.ScreenUtils;
import com.global.college.widget.daynight.DayNightHelper;


/**
 * Created by Administrator on 2020/2/24.
 * Describe: 基类Activity 主要封装公共标题栏，和容器布局
 */
public class BaseLayoutActivity extends FragmentActivity {

    protected AppCompatImageButton mBack;
    protected RelativeLayout mRlParent;
    private AppCompatTextView mTitle;
    protected AppCompatTextView mSubtitle;
    protected AppCompatTextView mSubtitle2;
    private FrameLayout mContentLayout;
    private Toolbar mToolbar;
    private boolean isTitleBarShow;

    protected DayNightHelper mDayNightHelper;
    public boolean mIsDay;
    protected boolean useImmersionBar = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        mDayNightHelper = new DayNightHelper();
//        mIsDay = mDayNightHelper.isDay();
//        setTheme(mIsDay ? R.style.DayTheme : R.style.NightTheme);
        super.setContentView(R.layout.activity_base_layout);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mBack = (AppCompatImageButton) findViewById(R.id.toolbar_back);
        mRlParent = findViewById(R.id.mRlParent);
        mTitle = (AppCompatTextView) findViewById(R.id.toolbar_title);
        mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
        mSubtitle = (AppCompatTextView) findViewById(R.id.toolbar_subtitle);
        mSubtitle2 = (AppCompatTextView) findViewById(R.id.toolbar_subtitle2);
        if (useImmersionBar) {
            ImmersionBar.with(this)
                    .statusBarColor(R.color.color_background)
                    .statusBarDarkFont(true)
                    .init();
        }else {
            hideSystembar();
            ImmersionBar.with(this)
                    .statusBarColor(R.color.transparent)
                    .statusBarDarkFont(true)
                    .init();
        }
        //默认显示标题栏
        setShowTitleBar(true);
        //默认不显示返回按钮
        setShowBack(false);
        //默认不显示子标题
        setShowSubTitle(false);
        //默认不显示子标题2
        setShowSubTitle2(false);
    }


    /**
     * 设置标题栏显示或者隐藏
     *
     * @param isShow true则显示,false为不显示
     */
    public boolean setShowTitleBar(boolean isShow) {
        if (mToolbar != null) {
            mToolbar.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
        isTitleBarShow = isShow;
        return isTitleBarShow;
    }

    /**
     * fitsSystemWindows = false
     */
    public void hideSystembar(){
        mRlParent.setFitsSystemWindows(false);
    }

    /**
     * 是否显示返回按钮
     *
     * @param isShow true则显示,false为不显示
     */
    public void setShowBack(boolean isShow) {
        if (!isTitleBarShow)
            return;
        if (mToolbar != null) {
            mBack.setVisibility(isShow ? View.VISIBLE : View.GONE);
            mBack.setOnClickListener(isShow ? new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();

                }
            } : null);

            if (isShow) {
                mTitle.setPadding(0, 0, 0, 0);
            } else {
                mTitle.setPadding(ScreenUtils.dp2px(this, 16), 0, 0, 0);
            }
        }

    }

    /**
     * 是否显示子标题2
     *
     * @param isShow true则显示,false为不显示
     */
    public void setShowSubTitle2(boolean isShow) {
        if (!isTitleBarShow)
            return;
        if (mSubtitle2 != null) {
            mSubtitle2.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 是否显示子标题
     *
     * @param isShow true则显示,false为不显示
     */
    public void setShowSubTitle(boolean isShow) {
        if (!isTitleBarShow)
            return;
        if (mSubtitle != null) {
            mSubtitle.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 设置标题内容
     *
     * @param titleId
     */
    public void setTitle(@StringRes int titleId) {
        if (!isTitleBarShow)
            return;
        mTitle.setText(titleId);
    }

    /**
     * 设置标题内容
     *
     * @param title
     */
    public void setTitle(CharSequence title) {
        if (!isTitleBarShow)
            return;
        mTitle.setText(title);
    }


    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 如果你的app可以横竖屏切换，并且适配4.4或者emui3手机请务必在onConfigurationChanged方法里添加这句话
//        ImmersionBar.with(this).init();
    }
}